﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebBanHang
{
    public partial class DonHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CurrentContext.IsLogged)
                {

                    if (Request.QueryString["MaDeal"] != null)
                    {
                        string MaDeal = Request.QueryString["MaDeal"].ToString();
                        string sql = string.Format("select * from Deal where MaDeal = '{0}'", MaDeal);
                        DataTable DATA = new DataTable();
                        DATA = DataProvider.LoadTable(sql);
                        lbGiaTien.Text = DATA.Rows[0]["GiaBan"].ToString();
                        lbTenSanPham.Text = DATA.Rows[0]["TenDeal"].ToString();
                        ddlSoLuong_SelectedIndexChanged(null, null);
                    }
                    else
                    {
                        Response.Redirect("~/GUI/Home.aspx");
                    }
                }
                else
                {
                    Response.Redirect("~/GUI/Login.aspx");
                }
            }
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlCommand sql = new SqlCommand("insert into DONDATHANG (USERNAME, NgayDat,ThanhTien, MaDeal, SoLuong) values (@UserName, @NgayDat, @ThanhTien, @MaDeal, @SoLuong)");
            SqlParameter[] pr = {new SqlParameter("@UserName", CurrentContext.UserName),
                                    new SqlParameter("@NgayDat", DateTime.Now.Date),
                                    new SqlParameter("@ThanhTien", int.Parse(lbTongTien.Text)),
                                    new SqlParameter("@MaDeal", Request.QueryString["MaDeal"].ToString()),
                                    new SqlParameter("@SoLuong", int.Parse(ddlSoLuong.SelectedItem.ToString()))};

            DataProvider.ExecuteNonQuery(sql, pr);
            lbThongBao.Text = "Đặt hàng thành công bạn có thể mua thêm sản phẩm khác :)";
        }

        protected void ddlSoLuong_SelectedIndexChanged(object sender, EventArgs e)
        {
            int TongTien =int.Parse(lbGiaTien.Text);
            TongTien = TongTien * (int.Parse(ddlSoLuong.SelectedItem.ToString()));
            lbTongTien.Text = TongTien.ToString();

        }
    }
}